<?php
	include('include/header.php');
	include("include/commands.php");
        
        //Check if we are coming to this page from an insert query.
        if ($_POST["fromInsert"] == "true") {
            
            //Instantiate connector.
            $connector = new CloudDBInterface();
            
            //Check if we need to add a new Expense_Type
            if ($_POST["newEType"] != "") {
                
                //Grab variables.
                $newETypeDesc = $_POST["newEType"];
                $newETypeOccur = $_POST["occurrence"];
                $newETypeOccurId = strtok($newETypeOccur, ":");
                
                //Create the query.
                $query =    "INSERT INTO Expense_Type (description, occurrenceId)
                            VALUES (\"$newETypeDesc\", $newETypeOccurId)";

                //Insert the tuple.
                $connector->insertQuery($query);
            }

            //Add the new Expense now.
            //Get variables.
            $cost = $_POST["cost"];

            //Get the appropriate expense type.
            if ($_POST["newEType"] != "") {

                $eType = $_POST["newEType"];
                
                //Get the id.
                $query =    "SELECT id
                            FROM Expense_Type
                            WHERE description = \"$eType\"";

                $resArray = $connector->selectQuery($query);
                $eTypeId = $resArray[0]["id"];
            } else {
                
                $eType = $_POST["expenseType"];
                $eTypeId = strtok($eType, ":");
            }
            
            

            //Create the query.
            $query =    "INSERT INTO Expenses (cost, expenseTypeId, date)
                        VALUES ($cost, $eTypeId, CURDATE())";

            //Insert the tuple.
            $connector->insertQuery($query);            
        }
        
	
        
        if(isset($_GET['sortBy'])) {

            if (strcmp($_GET['sortBy'], "Description") == 0) {
                
                $expenses = getExpensesDescriptionSort();
            } else if (strcmp($_GET['sortBy'], "Frequency") == 0) {
                
                $expenses = getExpensesFrequencySort();
            } else {
                
                $expenses = getExpenses();
            }
		
	} else {
            
            $expenses = getExpenses();
	}
?>
        
	<div class="body">
	
                
		<h1> Expense Log: </h1>
                <div class="border">
                    <form>
                    <br/>
                    Sort By:
                            <select name="sortBy">
                                    <option>NONE</option>
                                    <option>Description</option>
                                    <option>Frequency</option>
                            </select>
                    <input type="submit" value="Filter"/>
                    </p>
                    </form>
                </div>
                <br/>
                <div class="border">
                    <form id="newExpenseForm" action="expenses.php" method="post" onsubmit="return validateExpenseInsert();">
                        <h4>Add a New Expense</h4>
                        <p>
                            Cost* <input type="text" name="cost" id="cost"/><br/>
                            Expense Type <?php expenseTypes(); ?><br/>
                            or Add a New Expense Type <input type="text" name="newEType" id="newEType"/><br/>
                            Occurrence <?php occurrences(); ?><br/>
                            
                            <input type="hidden" name="fromInsert" id="fromInsert" value="true"/>
                            <input type="submit" value="Submit"/>
                        </p>
                    </form>
                </div>
                <br/>
		<?php 

			if(sizeof($expenses) > 0){
                            
					generateExpensesTable($expenses);
				}
				else{
					echo "<p> No Expense(s) Found. </p>";
				}
				
				function generateExpensesTable($expenses) {
                                    
					echo "<table>";
					echo "<tr>";
					echo "<th>ID</th>";
					echo "<th>Cost</th>";
					echo "<th>Description</th>";
                                        echo "<th>Frequency</th>";
					echo "</tr>";
					for ($i=0;$i < sizeof($expenses); $i++) {
						$row = $expenses[$i];
						echo "<tr>";
						echo "<td>" .$row['id']."</td>";
						echo "<td>" .$row['cost']."</td>";
						echo "<td>" .$row['description']."</td>";
                                                echo "<td>" .$row['frequency']."</td>";
						echo "</tr>";
					}
					echo "</table>";
				}
		?>
	</div>


<?php 
	include('include/footer.php'); 
?>